package com.ericsson.enap.modules.kpi.service.impl;

import com.ericsson.enap.modules.kpi.entity.EnipMeasInfo;
import com.ericsson.enap.modules.kpi.entity.ImsFiveKpi;
import com.ericsson.enap.modules.kpi.service.EnipKpiFileProcess;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class KpiFileProcessPSBCCFIVEImpl implements EnipKpiFileProcess {

    @Override
    public boolean needUncompress() {
        return false;
    }

    @Override
    public NetType getNetType() {
        return NetType.PSBCCFIVE;
    }

//    public File download(ImsFiveKpi imsFiveKpi, String localDir, Date date, int delay, Logger log, String typeStr){
//        log.info("===================================================  start download Files");
//        FTPInfoVO ftpInfo = new FTPInfoVO();
//        ftpInfo.setHost(imsFiveKpi.getIp4());
//        ftpInfo.setUserName(imsFiveKpi.getUsername());
//        ftpInfo.setPassword(imsFiveKpi.getPassword());
//        ftpInfo.setPath(imsFiveKpi.getDownloadPath());
//        ftpInfo.setPort(21);
//        SFtpCarrierUtil sftpCarrier = new SFtpCarrierUtil(ftpInfo, log);
//        File tempFile=null;
//        try {
//            sftpCarrier.connect();
//            Vector v = sftpCarrier.listFiles(ftpInfo.getPath());
//            if (v.size() > 0) {
//                Iterator it = v.iterator();
//                String filename="A20210426.1445+0800-1450+0800_APP-HZZZhnpsbcc1BER-01AER011.xml";
//                while (it.hasNext()) {
//                    ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) it.next();
//                    String fileTempName = entry.getFilename();
//                    SftpATTRS attrs = entry.getAttrs();
//                    if (!attrs.isDir()) {
//                        if (fileTempName.length() > 30 && fileTempName.endsWith(".xml"))
//                        {
//                            if(fileTempName.compareTo(filename)==1) {
//                                filename = fileTempName;
//                            }
//                        }
//                    }
//                }
//                tempFile=sftpCarrier.downloadFileKpi(ftpInfo.getPath(), filename, localDir, filename);
//            }
//        } catch (SftpException e) {
//            e.printStackTrace();
//        } finally {
//            sftpCarrier.disconnect();
//        }
//        return tempFile;
//    }
    public File download(ImsFiveKpi imsFiveKpi, String localDir, Logger log) {
        StringBuilder sb=new StringBuilder();
        Process proc = null;
        BufferedReader br;
        BufferedReader brError;
        String line;
        String lineError;
        try {
            String[] cmd = new String[3];
            cmd[0] = "sh";
            cmd[1] = "-c";
            cmd[2] = "/usr/bin/perl -w /opt/Ericsson/core/bin/get_last_second_file.pl";
            cmd[2] += " -node_ip " + imsFiveKpi.getIp4();
            cmd[2] += " -node_user '" + imsFiveKpi.getUsername() + "' -node_password '" + imsFiveKpi.getPassword();
            cmd[2] += "' -node_path " + imsFiveKpi.getDownloadPath();
            cmd[2] += " -chg_to_root_cmd " +"'sudo bash'";
            cmd[2] += " -dest_ip " +imsFiveKpi.getServerIp();
            cmd[2] += " -dest_user '" +imsFiveKpi.getServerUserName();
            cmd[2] += "' -dest_password '" +imsFiveKpi.getServerPassword();
            cmd[2] += "' -dest_path " + localDir;
            try {
                proc = Runtime.getRuntime().exec(cmd);
            } catch (IOException e) {
                e.printStackTrace();
            }
            br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            brError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
            try {
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                }
                while ((lineError = brError.readLine()) != null) {
                    sb.append(lineError);
                }
                proc.waitFor();
                System.out.println("proc end"+sb.toString());
            } catch (IOException e) {
                log.error(e.getMessage());
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        } finally {
            proc.destroy();
        }
        String result=sb.toString();
        if(null!=result&&!"".equals(result)&&result.contains("fiveKpi ")&&result.contains(" fiveKpi")){
            return new File(localDir+ File.separator+result.substring(result.indexOf("fiveKpi ")+8,result.lastIndexOf(" fiveKpi")));
        }else {
            return null;
        }
    }

    @Override
    public List<File> download(ImsFiveKpi imsFiveKpi, String localDir, Logger log, Date date, int delay) {
        return null;
    }

    @Override
    public Map<String, List<File>> download(ImsFiveKpi imsFiveKpi, String localDir, Logger log, Date date) {
        return null;
    }

    @Override
    public List<EnipMeasInfo> analysis(List<File> files, Logger log) {
        return null;
    }
}
